package com.gitee.grower.util.germ

import com.germ.germplugin.api.dynamic.gui.GermGuiPart
import com.germ.germplugin.api.dynamic.gui.GermGuiScroll
import com.gitee.grower.util.germ.germ.GermPartDSL
import taboolib.library.configuration.ConfigurationSection

class GermScrollDSL : GermPartDSL<GermGuiScroll>() {

    var invalid = false
    var relative = true
    var invalidV = true
    var invalidH = true

    var width = ""
    var height = ""
    var locationVX = ""
    var locationVY = ""
    var widthV = ""
    var heightV = ""
    var scrollableV = ""
    var scrolledV = ""

    var sliderV: GermPartDSL<*>? = null
    var sliderH: GermPartDSL<*>? = null

    var parts = mutableListOf<GermPartDSL<*>>()

    override val type: String = "scroll"


    override fun infuse(config: ConfigurationSection) {
        name = config.name

        config.getString("locationX")?.let { locationX = it }
        config.getString("locationY")?.let { locationY = it }
        config.getString("locationZ")?.let { locationZ = it }
        config.getString("width")?.let { width = it }
        config.getString("height")?.let { height = it }
        config.getString("locationVX")?.let { locationVX = it }
        config.getString("locationVY")?.let { locationVY = it }
        config.getString("widthV")?.let { widthV = it }
        config.getString("heightV")?.let { heightV = it }
        config.getString("scrollableV")?.let { scrollableV = it }
        config.getString("scrolledV")?.let { scrolledV = it }

        config.getConfigurationSection("scrollableParts")?.getKeys(false)?.forEach {
            parts += createDSL<GermPartDSL<*>>(config.getConfigurationSection("scrollableParts.$it")!!)
        }

        config.getConfigurationSection("sliderV")?.let {
            sliderV = createDSL<GermPartDSL<*>>(it) {
                it.name = "sliderV"
            }
        }
        config.getConfigurationSection("sliderH")?.let {
            sliderH = createDSL<GermPartDSL<*>>(it) {
                it.name = "sliderH"
            }
        }
        invalid = config.getBoolean("invalid")
        relative = config.getBoolean("relative")
        invalidV = config.getBoolean("invalidV")
        invalidH = config.getBoolean("invalidH")

        enable = config.getBoolean("enable")
    }


    override fun create(): GermGuiScroll {
        return GermGuiScroll(name).also {

            it.locationX = locationX
            it.locationY = locationY
            it.locationZ = locationZ
            it.enable = enable
            it.isInvalid = invalid
            it.isInvalidH = invalidH
            it.isInvalidV = invalidV
            it.isRelative = relative

            it.locationVX = locationVX
            it.locationVY = locationVY
            it.widthV = widthV
            it.heightV = heightV
            it.scrollableV = scrollableV
            it.scrolledV = scrolledV

            it.width = width
            it.height = height
            sliderV?.apply { it.sliderV = create() as GermGuiPart<*> }
            sliderH?.apply { it.sliderH = create() as GermGuiPart<*> }

            parts.map { it.create() as GermGuiPart<*> }.forEach(it::addGuiPart)

        }
    }


}